<?php

namespace App\Kuafu\Config\Model;

use App\Kuafu\Base\Model\Traits\KfSoftDeletes;
use App\Kuafu\Base\Model\Traits\KfUid;
use App\Kuafu\Config\Enum\ConfigCacheEnum;
use Database\Factories\ConfigFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\Cache;
use App\Kuafu\Base\Model\BaseModel;

class ConfigModel extends BaseModel
{
    use KfSoftDeletes, KfUid, HasFactory;

    protected $connection = 'mysql';
    protected $table      = 'config';
    protected $primaryKey = 'id';
    protected $fillable   = [
        'name',
        'key',
        'value',
        'type',
        'remark',
        'createdByUid',
        'updatedByUid',
    ];

    public static function newFactory()
    {
        return new ConfigFactory;
    }

    public static function getDataPage(string $name, string $key, string $type, int $pageSize, int $pageNum)
    {
        return self::query()
                   ->when($name, function ($q) use ($name) {
                       $q->where('name', "{$name}%");
                   })
                   ->when($key, function ($q) use ($key) {
                       $q->where('key', $key);
                   })
                   ->when($type, function ($q) use ($type) {
                       $q->where('type', $type);
                   })
                   ->orderByDesc('id')
                   ->paginate($pageSize, [], 'page', $pageNum);
    }

    public static function getDataPageByType(string $type): array
    {
        return Cache::remember(ConfigCacheEnum::getConfigType($type), 30, function () use ($type) {
            return self::query()
                       ->when($type, function ($q) use ($type) {
                           $q->where('type', $type);
                       })
                       ->orderByDesc('id')
                       ->get()
                       ->toArray();
        });
    }

    public static function getDataById(int $id)
    {
        return self::query()->where('id', $id)->first();
    }

    public static function addData(string $name, string $key, string $value, string $type, string $remark)
    {
        return self::query()->create([
            'name'   => $name,
            'key'    => $key,
            'value'  => $value,
            'type'   => $type,
            'remark' => $remark,
        ]);
    }

    public static function setData(int $id, string $name, string $key, string $value, string $type, string $remark)
    {
        return self::query()->where('id', $id)->update([
            'name'   => $name,
            'key'    => $key,
            'value'  => $value,
            'type'   => $type,
            'remark' => $remark,
        ]);
    }

    public static function delData(int $id)
    {
        return self::query()->where('id', $id)->update(['isDeleted' => 1]);
    }

    public static function delDatums(array $id)
    {
        return self::query()->whereIn('id', $id)->update(['isDeleted' => 1]);
    }

    /**
     * 删除 Type 对应的数据 [硬删除]
     *
     * @param string $type
     * @return mixed
     */
    public static function delDataByType(string $type)
    {
        return self::query()->where('type', $type)->delete();
    }

    public static function getConfigInfoByCondition($condition, $field)
    {
        return self::query()
                   ->where($condition)
                   ->select($field)
                   ->orderBy('id', 'DESC')
                   ->first();
    }

    public static function getConfigByTypeKey($type, $key)
    {
        return Cache::remember(ConfigCacheEnum::getConfigType($type, $key), getSwitchCacheTime(600), function () use ($type, $key) {
            return self::query()
                       ->where('type', $type)
                       ->where('key', $key)
                       ->orderByDesc('id')
                       ->get()
                       ->toArray();
        });
    }

    public static function getConfigByTypeKeyOne($type, $key, $ttl = 600)
    {
        return Cache::remember(ConfigCacheEnum::getConfigTypeOne($type, $key), getSwitchCacheTime($ttl),
            function () use ($type, $key) {
                return self::query()
                           ->where('type', $type)
                           ->where('key', $key)
                           ->first();
            });
    }
}
